<html>
  <head>
  <title>bustersAgents.py</title>
  </head>
  <body>
  <h3>bustersAgents.py (<a href="../bustersAgents.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># bustersAgents.py
# ----------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>util
<span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Agent
<span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Directions
<span style="color: blue; font-weight: bold">from </span>keyboardAgents <span style="color: blue; font-weight: bold">import </span>KeyboardAgent
<span style="color: blue; font-weight: bold">import </span>inference

<span style="color: blue; font-weight: bold">class </span>BustersAgent<span style="font-weight: bold">:
  </span><span style="color: red">"An agent that tracks and displays its beliefs about ghost positions."
  
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">( </span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">, </span>inference <span style="font-weight: bold">= </span><span style="color: red">"ExactInference"</span><span style="font-weight: bold">, </span>ghostAgents <span style="font-weight: bold">= </span><span style="color: blue">None </span><span style="font-weight: bold">):
    </span>inferenceType <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>lookup<span style="font-weight: bold">(</span>inference<span style="font-weight: bold">, </span>globals<span style="font-weight: bold">())
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>inferenceModules <span style="font-weight: bold">= [</span>inferenceType<span style="font-weight: bold">(</span>a<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>ghostAgents<span style="font-weight: bold">]
    
  </span><span style="color: blue; font-weight: bold">def </span>registerInitialState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: red">"Initializes beliefs and inference modules"
    </span><span style="color: blue; font-weight: bold">import </span>__main__
    <span style="color: blue">self</span><span style="font-weight: bold">.</span>display <span style="font-weight: bold">= </span>__main__<span style="font-weight: bold">.</span>_display
    <span style="color: blue; font-weight: bold">for </span>inference <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>inferenceModules<span style="font-weight: bold">: </span>inference<span style="font-weight: bold">.</span>initialize<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">)
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ghostBeliefs <span style="font-weight: bold">= [</span>inf<span style="font-weight: bold">.</span>getBeliefDistribution<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">for </span>inf <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>inferenceModules<span style="font-weight: bold">]
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>firstMove <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
    
  def </span>observationFunction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: red">"Removes the ghost states from the gameState"
    </span>agents <span style="font-weight: bold">= </span>gameState<span style="font-weight: bold">.</span>data<span style="font-weight: bold">.</span>agentStates
    gameState<span style="font-weight: bold">.</span>data<span style="font-weight: bold">.</span>agentStates <span style="font-weight: bold">= [</span>agents<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">]] + [</span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">, </span>len<span style="font-weight: bold">(</span>agents<span style="font-weight: bold">))]
    </span><span style="color: blue; font-weight: bold">return </span>gameState

  <span style="color: blue; font-weight: bold">def </span>getAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: red">"Updates beliefs, then chooses an action based on updated beliefs."
    </span><span style="color: blue; font-weight: bold">for </span>index<span style="font-weight: bold">, </span>inf <span style="color: blue; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>inferenceModules<span style="font-weight: bold">):
      </span><span style="color: blue; font-weight: bold">if not </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>firstMove<span style="font-weight: bold">: </span>inf<span style="font-weight: bold">.</span>elapseTime<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">)
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>firstMove <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
      </span>inf<span style="font-weight: bold">.</span>observeState<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">)
      </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ghostBeliefs<span style="font-weight: bold">[</span>index<span style="font-weight: bold">] = </span>inf<span style="font-weight: bold">.</span>getBeliefDistribution<span style="font-weight: bold">()
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>display<span style="font-weight: bold">.</span>updateDistributions<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ghostBeliefs<span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>chooseAction<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>chooseAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: red">"By default, a BustersAgent just stops.  This should be overridden."
    </span><span style="color: blue; font-weight: bold">return </span>Directions<span style="font-weight: bold">.</span>STOP

<span style="color: blue; font-weight: bold">class </span>BustersKeyboardAgent<span style="font-weight: bold">(</span>BustersAgent<span style="font-weight: bold">, </span>KeyboardAgent<span style="font-weight: bold">):
  </span><span style="color: red">"An agent controlled by the keyboard that displays beliefs about ghost positions."
  
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">, </span>inference <span style="font-weight: bold">= </span><span style="color: red">"ExactInference"</span><span style="font-weight: bold">, </span>ghostAgents <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span>KeyboardAgent<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index<span style="font-weight: bold">)
    </span>BustersAgent<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>index<span style="font-weight: bold">, </span>inference<span style="font-weight: bold">, </span>ghostAgents<span style="font-weight: bold">)
    
  </span><span style="color: blue; font-weight: bold">def </span>getAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">return </span>BustersAgent<span style="font-weight: bold">.</span>getAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">)
    
  </span><span style="color: blue; font-weight: bold">def </span>chooseAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">return </span>KeyboardAgent<span style="font-weight: bold">.</span>getAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">from </span>distanceCalculator <span style="color: blue; font-weight: bold">import </span>Distancer
<span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Actions
<span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Directions

<span style="color: blue; font-weight: bold">class </span>GreedyBustersAgent<span style="font-weight: bold">(</span>BustersAgent<span style="font-weight: bold">):
  </span><span style="color: red">"An agent that charges the closest ghost."
  
  </span><span style="color: blue; font-weight: bold">def </span>registerInitialState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: red">"Pre-computes the distance between every two points."
    </span>BustersAgent<span style="font-weight: bold">.</span>registerInitialState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">)
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>distancer <span style="font-weight: bold">= </span>Distancer<span style="font-weight: bold">(</span>gameState<span style="font-weight: bold">.</span>data<span style="font-weight: bold">.</span>layout<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
    
  </span><span style="color: blue; font-weight: bold">def </span>chooseAction<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gameState<span style="font-weight: bold">):
    </span><span style="color: darkred">"""
    First computes the most likely position of each ghost that 
    has not yet been captured, then chooses an action that brings 
    Pacman closer to the closest ghost (in maze distance!).
    
    To find the maze distance between any two positions, use:
    self.distancer.getDistance(pos1, pos2)
    
    To find the successor position of a position after an action:
    successorPosition = Actions.getSuccessor(position, action)
    
    livingGhostPositionDistributions, defined below, is a list of
    util.Counter objects equal to the position belief distributions
    for each of the ghosts that are still alive.  It is defined based
    on (these are implementation details about which you need not be
    concerned):

      1) gameState.getLivingGhosts(), a list of booleans, one for each
         agent, indicating whether or not the agent is alive.  Note
         that pacman is always agent 0, so the ghosts are agents 1,
         onwards (just as before).

      2) self.ghostBeliefs, the list of belief distributions for each
         of the ghosts (including ghosts that are not alive).  The
         indices into this list should be 1 less than indices into the
         gameState.getLivingGhosts() list.
     
    You may remove Directions.STOP from the list of available actions.
    """
    </span>pacmanPosition <span style="font-weight: bold">= </span>gameState<span style="font-weight: bold">.</span>getPacmanPosition<span style="font-weight: bold">()
    </span>legal <span style="font-weight: bold">= [</span>a <span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>gameState<span style="font-weight: bold">.</span>getLegalPacmanActions<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">if </span>a <span style="font-weight: bold">!= </span>Directions<span style="font-weight: bold">.</span>STOP<span style="font-weight: bold">]
    </span>livingGhosts <span style="font-weight: bold">= </span>gameState<span style="font-weight: bold">.</span>getLivingGhosts<span style="font-weight: bold">()
    </span>livingGhostPositionDistributions <span style="font-weight: bold">= [</span>beliefs <span style="color: blue; font-weight: bold">for </span>i<span style="font-weight: bold">,</span>beliefs
                                        <span style="color: blue; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ghostBeliefs<span style="font-weight: bold">)
                                        </span><span style="color: blue; font-weight: bold">if </span>livingGhosts<span style="font-weight: bold">[</span>i<span style="font-weight: bold">+</span><span style="color: red">1</span><span style="font-weight: bold">]]
    </span><span style="color: red">"*** YOUR CODE HERE ***"
    </span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()
</span>
  </pre>
  </body>
  </html>
  